package com.example.concurrent.AtomicReference;

import java.util.concurrent.atomic.AtomicReference;

/**
 * Created by SOAL on 2017/2/11 0011.
 */
public class AtomicReferenceDemo {
    public static void main(String[] args) {
        String initialReference = "the initially referenced string";
        AtomicReference atomicReference = new AtomicReference(initialReference);

        String initialReference2 = "the initially referenced string";
        AtomicReference<String> atomicStringReference2 =  new AtomicReference<String>(initialReference);

        String reference = (String) atomicReference.get();
        System.out.println(reference);

        atomicReference.set("New object referenced");
        System.out.println( atomicReference.get());

        AtomicReference<String> atomicStringReference =   new AtomicReference<String>(initialReference);

        String newReference = "new value referenced";
        boolean exchanged = atomicStringReference.compareAndSet(initialReference, newReference);
        System.out.println("exchanged: " + exchanged);

        exchanged = atomicStringReference.compareAndSet(initialReference, newReference);
        System.out.println("exchanged: " + exchanged);

    }
}
